import 'package:flutter/material.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter_demo/demo/route/detail.dart';

/// 1. 创建全局 eventBus 对象
final eventBus = EventBus();

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("Provider")),
        body: MyBody(),
      ),
    );
  }
}

class MyBody extends StatefulWidget {
  MyBody({Key? key}) : super(key: key);

  @override
  State<MyBody> createState() => _MyBodyState();
}

class _MyBodyState extends State<MyBody> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextButton(
        onPressed: () {
          Future res = Navigator.of(context)
              .push(MaterialPageRoute(builder: (context) => DetailPage(counter: counter + 1)));
          res.then((value) {
            setState(() {
              counter = value;
            });
          });
        },
        child: Text(
          "跳转到详情 当前计数 $counter",
          style: TextStyle(fontSize: 20),
        ),
      ),
    );
  }
}
